package restaurant;

import restaurant.iterators.Iterator;

/**
 * The Command object to request a table.
 * 
 * @author Odessa
 */

public class cmdRequestTable implements Command
{

	/** The info. */
	private InformationAggregator info;

	/** The name of the person requesting the table. */
	private String name;

	/** The number of people in the party. */
	private int numPeople;

	/** The number of tables required to sit the party. */
	private int numTables;



	/**
	 * Instantiates a new request table command.
	 * 
	 * @param info the information.
	 * @param numPeople the number of people in the party.
	 */
	public cmdRequestTable(InformationAggregator info, String name,
			int numPeople) {
		this.info = info;
		this.name = name;
		this.numPeople = numPeople;
	}



	/**
	 * Requests a table. If there are no empty tables, then the customer is
	 * added to the customer queue until a table becomes available. Otherwise, a
	 * table is assigned and the pin is generated for the table.
	 * 
	 * @return an array containing the table number and pin if a table is
	 *         assigned; otherwise returns the customer's number in the queue if
	 *         there isn't a table available.
	 * @see restaurant.Command#execute()
	 */
	@Override
	public Object execute()
	{
		Tables tables = info.getTables();
		numTables = (int) Math.ceil(numPeople / Table.CAPACITY);
		int pin = (int) (Math.random() * 9999) + 1000;

		if (tables.getNumEmpty() < numTables) {
			Customer c = new Customer(name, numTables);
			int numInQueue = info.addCustomerToQueue(c);

			return "No empty tables. You're number " + numInQueue +
					" in the queue.";
		}
		else {
			Iterator empty = tables.getEmptyTablesIterator();
			Table assignedTable = (Table) empty.next();
			int tableNo = assignedTable.getTableNo();

			tables.assignTable(tableNo);
			tables.setTablePin(tableNo, pin);

			String result = tableNo + "," + pin;

			return result;
		}
	}
}
